rm(list = ls())
load("results/pr_fcast_agg.rda")
library(cshapes)
library(dplyr)
pr_fcast_agg_grid <- pr_fcast_agg %>% 
  dplyr::slice_min(order_by = ebma, n = 15) %>% 
  dplyr::pull(gid)
gid <- pr_fcast_agg_grid
# produce a map for myanmar only
myanmar_map <- cshp(date = as.Date("2010-12-31"), useGW = T)
myanmar_map <- myanmar_map[which(myanmar_map@data$GWCODE==775),]

myanmar_map2 <- fortify(myanmar_map, region = "COWCODE")
load("data/myanmar_priogrid_cell.RData")
# select four gids
library(sf)
cooridates <- myanmar_priogrid_cell %>% 
  filter(gid %in% pr_fcast_agg_grid) %>%
  dplyr::select(gid, xcoord, ycoord)

cooridates_df <- cooridates %>%
  filter(gid == 158947 | gid ==159667 | gid == 158946 | gid == 159666 | gid == 162556)

library(ggrepel)
library(tidyverse)
ggplot() + 
  geom_polygon(data = myanmar_map2, 
               aes(x = long, y = lat, group = group) , 
              fill = "gray") + coord_fixed() +
  geom_point(data = cooridates, aes(x = xcoord, y = ycoord), 
             shape = 15, size = 6, color = "blue") +
  geom_text_repel(data = cooridates,
                  aes(xcoord, ycoord,label = gid, group = gid),
                  color = 'blue',
                  max.overlaps = 15,
                  size  = 6,
                  box.padding = 0.4, point.padding = 0.5) +
  scale_colour_distiller(type = "seq", palette = 1, na.value = "gray", guide = "colourbar") +
  scale_fill_manual(values = c("white","gray"), 
                    name= "", guide = guide_legend(reverse = TRUE)) + 
  labs(title = "",
       subtitle = "",
       y = "纬度（北纬）", x = "经度（东经）")+ theme_bw()+
  theme( axis.ticks.length = unit(0,"cm"),
         panel.spacing = unit(0, "lines"), 
         plot.title = element_text(hjust = 0.5),
         text = element_text(size=15, family = 'KaiTi'),
         plot.margin = unit(c(0,  0, 0, 0), "lines")) 

ggsave("figs/table_5a.png", width = 6, height = 8)


agg_grid <- pr_fcast_agg %>% 
  dplyr::slice_min(order_by = ebma, n = 15) %>% 
  dplyr::select(gid,  ebma)

agg_grid <- left_join(agg_grid, cooridates, by = "gid")
library(xtable)  
agg_grid$geometry <- NULL
agg_grid <- as.data.frame(agg_grid)
xtable(agg_grid,digits = 6) %>% 
    print(file = "figs/table5.html",type = "html")
